var typeMap = {
	"contract" : 1,
	"delivery" : 2,
	"ownership" : 3,
	"hostId" : 4,
	"proxyId" : 5
};
var upDocType = "";
$(function() {
	$(".subBar").click(function() {
		$(".subBar").removeClass("clicked");
		$(this).addClass("clicked");
		$(".bar").css("border-bottom", "0");
		var targetTable = "div " + "." + $(this).attr('data');
		upDocType = typeMap['' + $(this).attr('data')];
		$(".bar-table").hide();
		$(targetTable).show();
	});
	
	$("#updateContrach").click(function(){
		 updateContract();
	});
});

function plugin0() {
	return document.getElementById('plugin0');
}
plugin = plugin0;
function addEvent(obj, name, func) {
	if (obj.attachEvent) {
		obj.attachEvent("on" + name, func);
	} else {
		obj.addEventListener(name, func, false);
	}
}

function start() {
	document.cookie = "test=123;path=/";
	var test = -1;
	log(plugin().valid ? "有效.." : "无效");
	if (plugin().valid) {
		log("正在初始化...");
		plugin().SetDefaultDev(0);
		test = plugin().StartPreview();
		log("初始化" + (test != 1 ? "<span style='color:red'>失败</span>！" : "成功！"));
	} else {
		log("请检查是否安装了该浏览器插件！");
	}
	if (test == 1) {
		log("初始化摄像头参数...");
		invalidProperty();
		log("摄像头参数设置成功...");
	}
}

function invalidProperty() {
	plugin().SetResolution(0);// 设置分辨率为2592*1944
	plugin().SetScanSize(1);// 设置扫瞄尺寸A3
	plugin().SetVideoRotate(0);// 设置旋转角度0
	plugin().SetJpgQuality(50);// 设置JPG质量50
	plugin().SetRotateCrop(1);// 设置自动纠偏裁边
}

var imgPath;
function doPhoto() {
	// if (docType == "") {
	// Iw.Util.errorNoty("请选择证件类型！");
	// return;
	// }
	var dateStr = new Date().getTime();
	imgPath = "d:\\s600.jpg";
	var isOk = plugin().Scan(imgPath);
	if (!isOk) {
		showPhoto();
		eloamImg().AddImage(imgPath);
	} else
		log('拍照失败！');
}

/**
 * 上传图片和后续操作
 */
function upload() {
	var flag = validate();
	if (!flag)
		return;
	var key = "upload_pic_btn";
	disabledBtn(key);
	var sUrl = rootPath + "/doc/upload_order_doc.do?" + getParams();
	var isOk = plugin().UploadFileByHttp(sUrl, imgPath, "");
	if (isOk) {
		onCancel();
		Iw.Util.successNoty("上传成功！");
		refreshList();
	} else {
		Iw.Util.errorNoty("上传失败！");
	}
	removeDisabledBtn(key);
}
// 
// function validate() {
// var type = docType.replace(/[^0-9]/g,"");
// if (type == "") {
// Iw.Util.errorNoty("请选择拍照类型！");
// return false;
// }
// if ($("#docTypeName").textbox("getValue") == "") {
// Iw.Util.errorNoty("请输入文档名称！");
// return false;
// }
// if ($("#takePhoto").is(":disabled")) {
// Iw.Util.errorNoty("请按Ctrl+F5重刷页面！")
// return false;
// }
// return true;
// }

function getParams() {
	var type = upDocType;
	var num = $("#doc tbody tr:not(:hidden):last td:eq(0)").text().replace(
			/[^\d]/g, "");
	num = (num == "") ? 0 : parseInt(num) + 1;// 当前的数字
	var orderId = "$!orderDetail.order.id";
	var name = $("#docTypeName").textbox("getValue");
	return "docCategory=" + type + "&docCount=" + num + "&orderId=" + orderId
			+ "&docName=" + encodeURI(encodeURI(name));
}

function log(msg) {
	var html = $("#log").html();
	var array = html.split("<br>");
	if (array.length > 5) {
		var att0 = array[0];
		html = html.substring(att0.length + "<br>".length);
	}
	html += msg + "<br/>";
	$("#log").html(html);
}

function eloamImg() {
	return document.getElementById('eloamImg');
}

function CloseDevice() {
	plugin().CloseDevice();
}

// window.onunload=CloseDevice;
// window.onload=start;

// 上传弹出层
function showPhoto() {
	// var type = docType.replace(/[^0-9]/g,"");
	// var name = jQuery.trim($("div#docDiv h4.block .btn-group
	// label.active").text().replace(/\s/g, "").replace(/\(.*?\)/g, ""));
	//
	// if (type == 9) {
	// $("#docTypeName").textbox("setValue", "");
	// $("#docTypeName").textbox({editable:true});
	// } else {
	// $("#docTypeName").textbox("setValue", name);
	// $("#docTypeName").textbox({editable:false});
	// }

	scrollEasyuiVisible("photo_win");
	// $('#photo_win').window('move',{top:$(document).scrollTop() + 100});
}

function scrollEasyuiVisible(id) {
	$("#" + id).window({
		closed : false,
		modal : true,
		onOpen : function() {
			$(document.body).css({
				"overflow-x" : "hidden",
				"overflow-y" : "hidden"
			});
			return true;
		},
		onClose : function() {
			$(document.body).css({
				"overflow-x" : "auto",
				"overflow-y" : "auto"
			});
			return true;
		}
	});
	$('#' + id).window('move', {
		top : $(document).scrollTop() + 100
	});
}

function closeEasyuiWin(id) {
	$("#" + id).window("close");
}
function onCancel() {
	// $("#photo_view_win").modal("hide");
	closeEasyuiWin("photo_win");
}

function openScaner() {
	if ($(".clicked").length == 0) {
		$.messager.alert('Warning', '请选择文档类型！');
		return;
	}
	$("#openScaner").hide();
	$("#closeScaner").show();
	$("#scanContainer").show();
	start();
}

function closeScaner() {
	$("#openScaner").show();
	$("#closeScaner").hide();
	CloseDevice();
	$("#scanContainer").hide();
}

/**
 * 获取编辑字段的信息
 */
function getEditParam() {
	var param = {};
	var fireDate = $("[name='fireDate']").val();
	if (!$.trim(fireDate)) {
		param.error = '着火日期不能为空！';
		return param;
	}
	var mayContinueMonth = $("#mayContinueMonth").val();
	if (mayContinueMonth == 0) {
		param.error = '请选择可延长月数！';
		return param;
	}

	var payStartDate = $("[name='payStartDate']").val();
	if (!$.trim(payStartDate)) {
		param.error = '起始付款日期不能为空！';
		return param;
	}
	var identification = $("#identification").val();
	if (!$.trim(identification) || !/^\d{18}$/.test($.trim(identification))) {
		param.error = '请输入18位业主有效身份证号！';
		return param;
	}

	var landline = $("#landline").val();
	if (!$.trim(landline) || !/^\d{7,12}$/.test($.trim(landline))) {
		param.error = '请输入7-12位座机号！';
		return param;
	}
	var locationAddr = $("#locationAddr").val();
	if (!$.trim(locationAddr)) {
		param.error = '请输入业主现居住地！';
		return param;
	}

	var bankCityId = $("#bankCityId").val();
	if (bankCityId == 0) {
		param.error = '请选择开户城市！';
		return param;
	}
	var bankCityName = $("#bankCityId").find("option:selected").text();

	var bank = $("#bank").val();
	if (bank == '请选择') {
		param.error = '请选择银行！';
		return param;
	}
	var subBank = $("#subBank").val();
	if (!$.trim(subBank)) {
		param.error = '请输入开户支行！';
		return param;
	}
	var openName = $("#openName").val();
	if (!$.trim(openName)) {
		param.error = '请输入指定开户人！';
		return param;
	}
	var bankCode = $("#bankCode").val();
	if (!$.trim(bankCode) || !(/^\d{16,19}$/.test($.trim(bankCode)))) {
		param.error = '请输入16-19位指定账号！';
		return param;
	}
	var proxyName = $("#proxyName").val();
	var proxyIdentification = $("#proxyIdentification").val();
	if ($.trim(proxyName) || $.trim(proxyIdentification)) {
		if (!$.trim(proxyName)) {
			param.error = '请输入代理人姓名！';
			return param;
		}

		if (!$.trim(proxyIdentification)
				|| !/^\d{18}$/.test($.trim(proxyIdentification))) {
			param.error = '请输入18位代理人身份证号！';
			return param;
		}
	}

	var iwGroupLeaderMobile = $("#iwGroupLeaderMobile").val();
	if (!$.trim(iwGroupLeaderMobile)
			|| !(/^\d{11}$/.test($.trim(iwGroupLeaderMobile)))) {
		param.error = '请输入11位M1手机号！';
		return param;
	}

	param.contractId = contractId;
	param.fireDate = fireDate;
	param.mayContinueMonth = mayContinueMonth;
	param.payStartDate = payStartDate;
	param.identification = identification;
	param.landline = landline;
	param.locationAddr = locationAddr;
	param.bankCityId = bankCityId;
	param.bankCityName = bankCityName;
	param.bank = bank;
	param.subBank = subBank;
	param.openName = openName;
	param.bankCode = bankCode;
	param.proxyName = proxyName;
	param.proxyIdentification = proxyIdentification;
	param.iwGroupLeaderMobile = iwGroupLeaderMobile;
	return param;
}

function updateContract() {
	var param = getEditParam();
	
	if($.trim(param.error)){
		$.messager.alert('Warning',param.error);
		return;
	}
	$("#updateContrach").attr("disabled",true);
	$.post(rootPath+"/hostContract/update", 
			param
			, function(data) {
		console.log(data);
	});
}
